find_package(PkgConfig)
pkg_check_modules(PC_RTAUDIO rtaudio)

find_path(RTAUDIO_INCLUDE_DIRS
    NAMES rtaudio/RtAudio.h
    HINTS ${PC_RTAUDIO_INCLUDEDIR}
    ${PC_RTAUDIO_INCLUDE_DIRS}
)

find_library(RTAUDIO_LIBRARIES
    NAMES rtaudio
    HINTS ${PC_RTAUDIO_LIBDIR}
    ${PC_RTAUDIO_LIBRARY_DIRS}
)

if(RTAUDIO_INCLUDE_DIRS AND RTAUDIO_LIBRARIES)
    set(RTAUDIO_FOUND TRUE)
    set(RTAUDIO_LIBRARY ${RTAUDIO_LIBRARIES})
    set(RTAUDIO_INCLUDE_DIR ${RTAUDIO_INCLUDE_DIRS})
    set(RTAUDIO_VERSION ${PC_RTAUDIO_VERSION})
    include(FindPackageHandleStandardArgs)
    FIND_PACKAGE_HANDLE_STANDARD_ARGS(RTAUDIO FOUND_VAR RTAUDIO_FOUND VERSION_VAR RTAUDIO_VERSION
	    REQUIRED_VARS RTAUDIO_INCLUDE_DIRS RTAUDIO_INCLUDE_DIR RTAUDIO_LIBRARIES RTAUDIO_LIBRARY)

    mark_as_advanced(RTAUDIO_LIBRARIES RTAUDIO_LIBRARY RTAUDIO_INCLUDE_DIRS RTAUDIO_INCLUDE_DIR RTAUDIO_FOUND RTAUDIO_VERSION)

    if(NOT TARGET RTAUDIO::RTAUDIO)
        add_library(RTAUDIO::RTAUDIO UNKNOWN IMPORTED)
        set_target_properties(RTAUDIO::RTAUDIO PROPERTIES
          INTERFACE_COMPILE_DEFINITIONS "${_RTAUDIO_COMPILE_DEFINITIONS}"
          INTERFACE_INCLUDE_DIRECTORIES "${RTAUDIO_INCLUDE_DIRS}")
        if(EXISTS "${RTAUDIO_LIBRARY}")
          set_target_properties(RTAUDIO::RTAUDIO PROPERTIES
            IMPORTED_LINK_INTERFACE_LANGUAGES "C"
            IMPORTED_LOCATION "${RTAUDIO_LIBRARY}")
        endif()
    endif()
endif()

